<?php

class CmntNumCompare extends Module
{
    function __construct()
    {
    }    
    private static $_obj = null;
    static function getInstance()
    {
        if( !self::$_obj )
        {
            self::$_obj = new self();
        }

        return self::$_obj;
    }
    
    function get()
    {     
        $ret = array('ret'=>true, 'info'=>'succes');
        $data = array();
        $id = isset( $_POST['id'] ) ? trim( $_POST['id'] ) : '';   
        $url = NewsTools::getInstance()->getJumpUrl( $id );
        $this->info( $id.",".$url );
        if( !$url )
        {
            $ret['info'] = "获取底层页url失败。";
            $ret['ret'] = false;
            echo json_encode( $ret );
        }
        $data['url'] = $url;
        $this->info( $url );
        $comurl = "http://tcomment.qq.com/wb/essrtex/fetchAction?l=1&cn=20&url={$url}";
        $rootID = NewsTools::getInstance()->getRootID( $id, $comurl );
        $weibourl = "http://t.qq.com/p/t/{$rootID}";
        $data['weibo'] = $weibourl;
        $ret['data'] = $data;
        echo json_encode( $ret );
    }
    
    function getTUrl()
    {
        $ret = array('ret'=>true, 'info'=>'succes');
        $id = isset( $_POST['id'] ) ? trim( $_POST['id'] ) : '';
        $rootid = NewsTools::getInstance()->getRootID( $id );
        $ret['data'] = "http://t.qq.com/p/t/{$rootid}";
        echo json_encode( $ret );
    }
    
    function getUrl()
    {
        $ret = array('ret'=>true, 'info'=>'succes');
        $id = isset( $_POST['id'] ) ? trim( $_POST['id'] ) : '';   
        $url = NewsTools::getInstance()->getJumpUrl( $id );
        $ret['data'] = $url;
        $this->info( $id.", ".$url );
        echo json_encode( $ret );
    }
    
    private function getList( $chlid )
    {
        $url = "http://inews.qq.com/getQQNewsIndexAndItems?chlid={$chlid}";
        $info = file_get_contents( $url );
        return json_decode( $info , true );
    }
    private function getIdList( $chlid )
    {
        //$url = "http://inews.qq.com/getQQNewsIndex?chlid={$chlid}";
        $url = "http://inews.qq.com/getQQNewsIndexAndItems?chlid={$chlid}";
		$this->info( __LINE__ .print_r( $url, true ) );
        $info = file_get_contents( $url );
        
        $arr = json_decode( $info , true );
        if( $arr == false )
        {
            throw new Exception( "can't get chlid index:{$chlid}" );
        }
        return $arr['idlist'][0];
    }
    
    function getNews()
    {
        $ids = isset( $_POST['ids'] ) ? trim( $_POST['ids'] ) : '';
        $info = file_get_contents( "http://inews.qq.com/getQQNewsListItems?ids=".urlencode( $ids ) );
        $ret = json_decode( $info, true );
        if( ! $ret || $ret['ret'] )
        {
            $this->info( print_r( $ret, true ) );
            echo json_encode( array('ret'=>0, 'info'=>'fail.' ) );
            return;
        }
        
        $detail = "<html><body><pre>".print_r( $ret['newslist'][0], true )."</pre></body></html>";            
        //$detail = htmlentities( $info, ENT_QUOTES, 'UTF-8' );
        echo json_encode( array( 'ret'=>true, 'data'=>$detail ) );
    }
    
    
    function getWebCmntUrl()
    {
        $id = isset( $_POST['id'] ) ? trim( $_POST['id'] ) : '';
        $cmd_id = NewsTools::getInstance()->getCommentID( $id );
        $url = "http://inews.qq.com/getQQNewsComment?page=1&comment_id={$cmd_id}&url=http%3A%2F%2Fview.inews.qq.com%2Fa%2F{$id}&article_id={$id}";
        echo json_encode( array( 'ret'=>true, 'data'=>$url ) );
    }
    
    function getWeiboCmntUrl()
    {
        $id = isset( $_POST['id'] ) ? trim( $_POST['id'] ) : '';
        $cmd_id = NewsTools::getInstance()->getCommentID( $id );
        $chlid = NewsTools::getInstance()->getChlid( $id );
        $url = "http://inews.qq.com/getQQNewsComment?page=1&comment_id={$cmd_id}&url=http%3A%2F%2Fview.inews.qq.com%2Fa%2F{$id}&article_id={$id}&chlid={$chlid}";
        echo json_encode( array( 'ret'=>true, 'data'=>$url ) );
    }
    
    function getCmntsList()
    {
        $id = isset( $_GET['id'] ) ? trim( $_GET['id'] ) : '';
        $page = isset( $_GET['page'] ) ? trim( $_GET['page'] ) : 1;
        $cmd_id = NewsTools::getInstance()->getCommentID( $id );
        $chlid = NewsTools::getInstance()->getChlid( $id );
        $url = "http://inews.qq.com/getQQNewsComment?page={$page}&comment_id={$cmd_id}&url=http%3A%2F%2Fview.inews.qq.com%2Fa%2F{$id}&article_id={$id}&chlid={$chlid}";
        
        
        $str = file_get_contents( $url );
        if( $str == '' )
        {
            throw new Exception( "打开评论列表失败" );
        }
        $arr = json_decode( $str, true );
        if( $arr == false )
        {
            throw new Exception( "解析json失败" );
        }
        //print_r( $arr );
        if( isset( $arr['comments']['hot'] ) && $arr['comments']['hot'] ) foreach( $arr['comments']['hot'] as &$one )
        {
            if( $one ) foreach( $one as &$two )
            {
                if( isset( $two['reply_id'] ) && $two['reply_id'] )
                {
                    $two['reply_id'] = number_format( $two['reply_id'], 0, '', '' );
                    $two['reply_content'] = htmlentities ( $two['reply_content'], ENT_QUOTES, 'UTF-8' );
                    $two['nick'] = htmlentities ( $two['nick'], ENT_QUOTES, 'UTF-8' );
                }
            }
            $one = array_reverse( $one );
        }
        if( isset( $arr['comments']['new'] ) && $arr['comments']['new'] ) foreach( $arr['comments']['new'] as &$one )
        {
            if( $one ) foreach( $one as &$two )
            {
                if( isset( $two['reply_id'] ) && $two['reply_id'] )
                {
                    $two['reply_id'] = number_format( $two['reply_id'], 0, '', '' );
                    $two['reply_content'] = htmlentities ( $two['reply_content'], ENT_QUOTES, 'UTF-8' );
                    $two['nick'] = htmlentities ( $two['nick'], ENT_QUOTES, 'UTF-8' );
                }
            }
            $one = array_reverse( $one );
        }
        if( $arr['comments']['new'] ) $arr['bnext'] = 1;
        $arr['url'] = $url;
        $newsInfo = array();
        NewsTools::getInstance()->selectNews( array( $id ), $newsInfo );
        if( $newsInfo && isset( $newsInfo[$id] ) )
        {
            $arr['title'] = htmlentities ( $newsInfo[$id], ENT_QUOTES, 'UTF-8' );
        }
        
        parent::smartyOut( 'commentlist', $arr );
    }
    
    function getContent()
    {
        $id = isset( $_GET['id'] ) ? trim( $_GET['id'] ) : '';
        $rets = NewsTools::getInstance()->getContent( $id );
        
        $newsInfo = array();
        NewsTools::getInstance()->selectNews( array( $id ), $newsInfo );
        if( $newsInfo && isset( $newsInfo[$id] ) )
        {
            $rets['title'] = htmlentities ( $newsInfo[$id], ENT_QUOTES, 'UTF-8' );
        }
        
        parent::smartyOut( 'newscontent', $rets );
    }
    
    function getCmntNum()
    {
        $ret = array( 'ret'=>true );
        $chlid = isset( $_POST['chlid'] ) ? trim( $_POST['chlid'] ) : 'news_news_digi';
        if( ! $chlid  )
        {
            $ret = array( 'ret'=>false, 'info'=>'need chlid.' );
            echo json_encode( $ret );
        }
        $alist = $this->getIdList( $chlid );
		if( ! $alist )
		{
            $ret = array( 'ret'=>false, 'info'=>"can't get chlid:{$chlid}\n." );
            echo json_encode( $ret );
		}
        
        // 限制前50篇和10条评论之下的文章；
        $tmp = array();
        foreach( $alist['ids'] as $k=>$one )
        {
            if( $k >= 50 ) break;
            if( $one['comments'] <= 10 )
            {
                $one['index'] = $k;
                $tmp[] = $one;
            }
        }
        $alist['ids'] = $tmp;
        
        $alist['chlid'] = $chlid;
        $ids = array();
        $info = date( "Y-m-d H:i:s" )."\n";
        foreach( $alist['ids'] as &$one )
        {
            $rets = NewsTools::getInstance()->getCommentList( $one['id'] );
            if( ! isset( $rets['comments'] ) ) 
            {
                $info .= "{$one['index']},{$one['id']},理论{$one['comments']},something wrong.\n";
                continue;
            }
            $one['nact'] = count( $rets['comments']['new'] );
            if( $one['comments'] != $one['nact'] )
            {
                $info .= "{$one['index']},{$one['id']},理论{$one['comments']},实际{$one['nact']}";
                $nsite = 0;
                $nweibo = 0;
                foreach( $rets['comments']['new'] as $two )
                {
                    $end = end( $two );
                    if( $end['from'] == 'weibo_new' )
                    {
                        $nweibo ++;
                    }
                    elseif( $end['from'] == 'local_new' )
                    {
                        $nsite ++;
                    }
                }
                $info .= ",site:{$nsite},weibo:{$nweibo}\n";
            }
        }
        $ret = array( 'ret'=>true , 'data'=>$info );
        echo json_encode( $ret );
    }

    function display()
    {
        parent::smartyOut( 'cmntnumcompare' );
    }
}
























